package com.jie.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ErrorMessageConfig {
    /**
     * @description:异常消息交换机
     * @author: jie
     * @time: 2022/2/26 18:39
     */
    @Bean
    public DirectExchange errorMessageExchange() {
        return new DirectExchange("error.direct");
    }
    /**
     * @description:队列
     * @author: jie
     * @time: 2022/2/26 18:39
     */
    @Bean
    public Queue errorQueue(){
        return new Queue("err.queue");
    }
    /**
     * @description:将交换机和队列绑定
     * @author: jie
     * @time: 2022/2/26 18:39
     */
    @Bean
    public Binding errorMessageBinding(){
        return BindingBuilder
                .bind(errorQueue())
                .to(errorMessageExchange())
                .with("error");
    }
    /**
     * @description:异常消息处理器
     * @author: jie
     * @time: 2022/2/26 18:41
     */
    @Bean
    public MessageRecoverer republishMessageReciverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate,"error.direct","error");
    }
}
